package com.wondream.jenkins.service;

import com.wondream.jenkins.JenkinsServer;
import com.wondream.jenkins.model.*;

import java.io.IOException;

/**
 * @author wangjun
 * @note JenkinsAutomation
 * @date 2025-01-02  17:24
 */
public class JenkinsAutomation {

    private static final Long DEFAULT_RETRY_INTERVAL = 200L;

    public static void monitorJob(JenkinsServer jenkins,
                                  JobWithDetails job,
                                  String jobName,
                                  QueueReference reference) {
        new JenkinsAutomation().doMonitorJob(jenkins, job, jobName, reference);
    }

//    @Async
    public void doMonitorJob(JenkinsServer jenkins,
                                  JobWithDetails job,
                                  String jobName,
                             QueueReference reference) {
        try {
            BuildOnlyDetails buildWithDetails = queryDetails(jenkins, jobName, reference);
            System.out.println("Build result: " + buildWithDetails.getResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private BuildOnlyDetails queryDetails(JenkinsServer server, String jobName, QueueReference queueRef)
            throws IOException, InterruptedException {
        JobWithDetails job = server.getJob(jobName);
        QueueItem queueItem = server.getQueueItem(queueRef);

        while (!queueItem.isCancelled() && job.isInQueue()) {
            Thread.sleep(DEFAULT_RETRY_INTERVAL);
            job = server.getJob(jobName);
            queueItem = server.getQueueItem(queueRef);
        }

        Build build = server.getBuild(queueItem);
        if (queueItem.isCancelled()) {
            return build.justDetails();
        }

        boolean isBuilding = build.justDetails().isBuilding();
        while (isBuilding) {
            Thread.sleep(DEFAULT_RETRY_INTERVAL);
            isBuilding = build.justDetails().isBuilding();
        }

        return build.justDetails();
    }

    public static BuildResult queryJobResult(JenkinsServer server, String jobName, QueueReference queueRef)
            throws IOException, InterruptedException {
        JobWithDetails job = server.getJob(jobName);
        QueueItem queueItem = server.getQueueItem(queueRef);

        // 排队等待
        while (!queueItem.isCancelled() && job.isInQueue()) {
            Thread.sleep(DEFAULT_RETRY_INTERVAL);
            job = server.getJob(jobName);
            queueItem = server.getQueueItem(queueRef);
        }

        Build build = server.getBuild(queueItem);
        if (queueItem.isCancelled()) {
            return BuildResult.CANCELLED;
        }
        System.out.println("buildUrl: "+ build.getUrl());
        BuildOnlyDetails details = build.justDetails();
        boolean isBuilding = details.isBuilding();
        if (isBuilding) {
            return BuildResult.BUILDING;
        }

        return details.getResult();
    }

    public static BuildResult queryJobResult(JenkinsServer server, String jobName, String buildUrl)
            throws IOException, InterruptedException {
        JobWithDetails job = server.getJob(jobName);
        Build build = job.getLastBuild();
        System.out.println("lastBuildUrl: "+ build.getUrl());
        System.out.println("currentBuildUrl: "+ buildUrl);
        BuildOnlyDetails details = build.justDetails(buildUrl);
        boolean isBuilding = details.isBuilding();
        if (isBuilding) {
            return BuildResult.BUILDING;
        }
        return details.getResult();
    }
}
